using DesignMix.Gesture;
using System.Diagnostics;

namespace DesignMix;

public partial class DesignContent : ContentView
{
    double initScale = 1;
    double initX = -10000, initY = -10000;
    double initWidth = 20000, initHeight = 20000;
    DateTime lastTap = DateTime.MinValue;
    double mousePointX = 0, mousePointY = 0;
    public DesignContent()
	{
		InitializeComponent();
	}
    private GestureRecognizerView? gestureRecognizer;
    public GestureRecognizerView? GestureRecognizer
    {
        get => gestureRecognizer;
        set
        {
            gestureRecognizer = value;
            if (gestureRecognizer != null)
            {
                gestureRecognizer.TapGestureListener += RecognizerView_TapGestureListener;
                gestureRecognizer.PointerGestureListener += RecognizerView_PointerGestureListener;
                gestureRecognizer.PanGestureListener += RecognizerView_PanGestureListener;
                gestureRecognizer.PinchGestureListener += RecognizerView_PinchGestureListener;
                gestureRecognizer.MouseListener += RecognizerView_MouseListener;
            }
        }
    }
    private void RecognizerView_MouseListener(object sender, MouseEventArgs args)
    {
        if (args.Status == MouseRecognizerStatus.WheelMoved)
        {
            if (args.MouseWheelDelta < 0)
            {
                if (initScale < 0.1)
                {
                    initScale = 0.1;
                }
                else
                {
                    initScale -= 0.05;
                }

            }
            else if (args.MouseWheelDelta > 0)
            {
                if (initScale > 10)
                {
                    initScale = 10;
                }
                else
                {
                    initScale += 0.05;
                }
            }
            drawView.Scale = initScale;
            //Debug.WriteLine("Scale+++++++++++++++++++" + designView.Scale);
        }
    }

    private void RecognizerView_PinchGestureListener(object sender, PinchGestureEventArgs args)
    {
        switch (args.Status)
        {
            case GestureRecognizerStatus.Started:
                initScale = drawView.Scale;
                break;
            case GestureRecognizerStatus.Running:
                drawView.Scale += args.ScaleIncrement;
                break;
            case GestureRecognizerStatus.Complete:
                drawView.Scale += args.ScaleIncrement;
                break;
            case GestureRecognizerStatus.Cancel:
                drawView.Scale = initScale;
                break;
        }
        //Debug.WriteLine($"Pinch status={args.Status} Scale={args.Scale} ScaleIncrement={args.ScaleIncrement}");
    }

    private void RecognizerView_PanGestureListener(object sender, PanGestureEventArgs args)
    {
        //if (args.Status != GestureRecognizerStatus.Running)
        //    Debug.WriteLine($"Pan status={args.Status} TotalX={args.TotalX} TotalY={args.TotalY} IncX={args.IncX} IncY={args.IncY}");
        switch (args.Status)
        {
            case GestureRecognizerStatus.Started:
                initScale = drawView.Scale;
                initX = drawView.TranslationX * initScale;
                initY = drawView.TranslationY * drawView.Scale;

                Debug.WriteLine("start++++++++++++++++++++++" + initX + "+++++++" + initY);
                break;
            case GestureRecognizerStatus.Running:
                PanMoveDesignView(args);
                Debug.WriteLine("Running++++++++++++++++++++" + initX + "+++++" + initY);
                break;
            case GestureRecognizerStatus.Complete:

                PanMoveDesignView(args);
                initX = drawView.TranslationX * Scale;
                initY = drawView.TranslationY * Scale;
                Debug.WriteLine("Complete++++++++++++++++++++++++" + initX + "++++++++" + initY);
                break;
            case GestureRecognizerStatus.Cancel:
                drawView.TranslationX = initX;
                drawView.TranslationY = initY;
                break;
        }
    }
    private void PanMoveDesignView(PanGestureEventArgs args)
    {
        if (initX + args.TotalX > 0)
        {
            drawView.TranslationX = 0;
        }
        //else if (initX + args.TotalX < (-initWidth * initScale + xCoordinateView.Width))
        //{
        //    designView.TranslationX = -initWidth * initScale + xCoordinateView.Width;
        //}
        //else
        //{
        //    designView.TranslationX = initX + args.TotalX;
        //}
        //if (initY + args.TotalY > 0)
        //{
        //    designView.TranslationY = 0;
        //}
        //else if (initY + args.TotalY < (-initHeight * initScale + yCoordinateView.Height))
        //{
        //    designView.TranslationY = -initHeight * initScale + yCoordinateView.Height;
        //}
        //else
        //{
        //    designView.TranslationY = initY + args.TotalY;
        //}



    }



    private void RecognizerView_PointerGestureListener(object sender, PointerGestureEventArgs args)
    {

        // Debug.WriteLine($"Pointer status={args.Status} X={args.X} Y={args.Y} Pressed={args.Pressed}");
    }

    private void RecognizerView_TapGestureListener(object sender, TapGestureEventArgs args)
    {
        if (args.Status == GestureRecognizerStatus.Complete)
        {
            if ((DateTime.Now - lastTap).TotalMilliseconds <= 500)
            {
                drawView.TranslationX = drawView.TranslationY = -10000;
                drawView.Scale = 1;
                drawView.Rotation = 0;
                Debug.WriteLine("tap+++++++++++++++++++++++" + drawView.Scale);
            }
            lastTap = DateTime.Now;
        }
        //Debug.WriteLine($"Tap status={args.Status} X={args.X} Y={args.Y} Pressure={args.Pressure}");
    }
}